package com.dhcc.bpm.modules.safe.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.bpm.common.text.Convert;
import com.dhcc.bpm.modules.safe.entity.ApiControl;
import com.dhcc.bpm.modules.safe.mapper.ApiControlMapper;
import com.dhcc.bpm.modules.safe.service.IApiControlService;
import com.dhcc.bpm.modules.safe.vo.ApiControlVO;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ApiControlServiceImpl
 * @Description 访问控制API关联表 服务实现类
 * @Author daina
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@Service("opc_api_control")
public class ApiControlServiceImpl extends ServiceImpl<ApiControlMapper, ApiControl> implements IApiControlService {

	@Autowired
	private ApiControlMapper apiControlMapper;

	@Override
	public IPage<ApiControlVO> selectApiControlPage(IPage<ApiControlVO> page, ApiControlVO apiControl) {
		return page.setRecords(baseMapper.selectApiControlPage(page, apiControl));
	}

	/**
	 * 批量删除api关联
	 * @param opcControlId
	 * @param ids
	 * @return
	 */
	@Override
	public int deleteApis(Long opcControlId, String ids) {

		return apiControlMapper.deleteApis(opcControlId, Convert.toLongArray(ids));
	}

	/**
	 * 批量添加api
	 * @param opcControlId
	 * @param ids
	 * @return
	 */
	@Override
	public Boolean batchControllerApi(Long opcControlId, String ids) {
        List<Long> apiIds = Func.toLongList(ids);
        ArrayList<ApiControl> list = new ArrayList<>();
        apiIds.forEach(apiId->{
            ApiControl ma = new ApiControl();
            ma.setOpcApiId(apiId);
            ma.setOpcControlId(opcControlId);
            list.add(ma);
        });
		return saveBatch(list);
	}


}
